import {Store,Module,ActionContext} from 'vuex'
import ListModule from './list-module'
import ListState from './list-state'
import UiMenu from '../entities/uiMenu'
import MenuElement from '../entities/menuElement'
import PermissionTree from '../entities/permissionTree'

import Role from '../entities/role'
import Ajax from '../../lib/ajax'
import PageResult from '@/store/entities/page-result';
import ListMutations from './list-mutations'

interface UiMenuState extends ListState<UiMenu>{
    editUiMenu:UiMenu
}
interface UiMenuEleState extends ListState<MenuElement>{
    editEleUiMenu:MenuElement
}
class UserMutations extends ListMutations<UiMenu>{

}
class UiMenuModule extends ListModule<UiMenuState,any,UiMenu>{
    state={
        totalCount:0,
        currentPage:1,
        pageSize:10,
        list: new Array<UiMenu>(),
        listTree:null,
        loading:false,
        editUiMenu:new UiMenu(),
        editUiMenuEle:new MenuElement(),
        roles:new Array<Role>()
    }
    actions={
        async getAll(context:ActionContext<UiMenuState,any>,payload:any){
            context.state.loading=true;
            let reponse=await Ajax.get('/api/services/app/User/GetAll',{params:payload.data});
            context.state.loading=false;
            let page=reponse.data.result as PageResult<UiMenu>;
            context.state.totalCount=page.totalCount;
            context.state.list=page.items;
        },
        async getAllTree(context:ActionContext<UiMenuState,any>,payload:any){
            context.state.loading=true;
            let reponse=await Ajax.get('/api/services/app/UiMenu/GetPagedTree',{params:payload.data});
            context.state.loading=false;
            let page=reponse.data.result ;
            context.state.totalCount=page.totalCount;
            return page.items;
        },
        async create(context:ActionContext<UiMenuState,any>,payload:any){
            
            return await Ajax.post('/api/services/app/UiMenu/Create',payload.data);
        },
        async createEle(context:ActionContext<UiMenuEleState,any>,payload:any){
            return   await Ajax.post('/api/services/app/MenuElement/Create',payload.data);
        },
        async update(context:ActionContext<UiMenuState,any>,payload:any){
            return  await Ajax.put('/api/services/app/UiMenu/Update',payload.data);
        },
        async updateEle(context:ActionContext<UiMenuEleState,any>,payload:any){
            return  await Ajax.put('/api/services/app/MenuElement/Update',payload.data);
        },
        async delete(context:ActionContext<UiMenuState,any>,payload:any){
            await Ajax.delete('/api/services/app/UiMenu/Delete?Id='+payload.data.id);
        },
        async deleteEle(context:ActionContext<UiMenuEleState,any>,payload:any){
            await Ajax.delete('/api/services/app/MenuElement/Delete?Id='+payload.data.id);
        },
        async getPermissionTree(){
            let reponse=await Ajax.get('/api/services/app/UiMenu/getPermissionTree');
            return reponse.data.result as PermissionTree;
        },
        async getpageTree(context:ActionContext<UiMenuState,any>,payload:any){
            let reponse=await Ajax.get('/api/services/app/UiMenu/GetPagedTree',{params:payload.data});
            return reponse.data.result ;
        },
        async getElePaged(context:ActionContext<UiMenuState,any>,payload:any){
            let reponse=await Ajax.get('/api/services/app/MenuElement/GetPaged?uiMenuId='+payload.data.id);
            return reponse.data.result ;
        },
        async getAllUiMenuEle(){
            let reponse=await Ajax.get('/api/services/app/MenuElement/getAllUiMenuEle');
            return reponse.data.result ;
        }
        
        
        
    };
    mutations={
        setCurrentPage(state:UiMenuState,page:number){
            state.currentPage=page;
        },
        setPageSize(state:UiMenuState,pagesize:number){
            state.pageSize=pagesize;
        },
        edit(state:UiMenuState,uiMenu:UiMenu){
            state.editUiMenu=uiMenu;
        },
        editEle(state:UiMenuEleState,uiMenuEle:MenuElement){
            state.editEleUiMenu=uiMenuEle;
        }
        
    }
}
const uiMenuModule=new UiMenuModule();
export default uiMenuModule;